<?php


namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\Favorites;
use app\common\model\Goods;
use app\common\model\Orders;
use think\Exception;

class Talent extends Api
{
    protected $noNeedLogin = '';
    protected $noNeedRight = '*';

    /**
     * @var $model Goods
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new Goods();
    }

    /**
     * 发布服务
     */
    public function publish()
    {
        $param = input('param.');
        $validate = new \app\api\validate\Talent();
        if (!$validate->check($param)){
            $this->error($validate->getError());
        }
        if (isset($param['id'])){
            if ($row = $this->model::get($param['id'])){
                if ($row->user_id != $this->auth->id){
                    $this->error('无权限');
                }
                unset($param['id']);
                $param['verify_status'] = 'await';
                $row->save($param);
                $this->success('修改成功');
            } else {
                $this->error('参数异常');
            }
        }
        $user = $this->auth->getUser();
        $param['shop_id'] = $user->shop->id;
        $user->goods()->save($param);
        $this->success('发布成功');
    }

    /**
     * 服务列表
     */
    public function lists()
    {
        [$where, $sort, $order, $offset, $limit] = $this->buildparams($this->model, '', '', '');
        $total = $this->model
            ->where($where)
            ->where('verify_status','=', 'allow')
            ->where('onshelf_status','=', '1')
            ->count();
        $list = $this->model
            ->where($where)
            ->where('verify_status','=', 'allow')
            ->where('onshelf_status','=', '1')
            ->with('user')
            ->order($sort, $order)
            ->limit($offset, $limit)
            ->select();

        $result = array("total" => $total, "rows" => $list);
        $this->success('', $result);
    }

    /**
     * 服务详情
     */
    public function detail()
    {
        $id = input('param.id',0, 'intval');
        if (!$id){
            $this->error('参数错误');
        }
        if (!$row = $this->model->with('user.enterprise')->find($id)) {
            $this->error('未找到该服务');
        }
        if ($row->user_id != $this->auth->id && ($row->verify_status != 'allow' || $row->onshelf_status != 1)){
            $this->error('该服务无法展示');
        }
        $row->own_favorites = Favorites::where(['user_id'=> $this->auth->id,'collect_name'=>'goods','collect_id'=>$id])->find() ? 1 : 0;
        $this->success('', $row);
    }

    /**
     * 雇佣
     */
    public function hire()
    {
        $id = input('param.id',0, 'intval');
        $quantity = input('param.quantity',0, 'intval');
        if (!$id || !$quantity || $quantity <= 0) {
            $this->error('参数错误');
        }
        if (!$row = $this->model->find($id)) {
            $this->error('未找到该需求');
        }
        if ($row->user_id == $this->auth->id){
            $this->error('无法雇佣自己');
        }
        if ($row->verify_status != 'allow' || $row->onshelf_status != 1){
            $this->error('无法雇佣');
        }
        $data = [
            'goods_id'  => $id,
            'aid'       => $this->auth->id,
            'bid'       => $row->user_id,
            'title'     => $row->title,
            'quantity'  => $quantity,
            'price'     => $row->price,
            'amount'    => $quantity * $row->price,
            'verify_status'=> 'allow'
        ];
        Orders::create($data);
        $this->success('发起雇佣成功');
    }

    /**
     * 我的服务统计
     * @throws Exception
     */
    public function mycount()
    {
        $user = $this->auth->getUser();
        $goods['on'] = $user->goods()->where(['onshelf_status'=>'1'])->count();
        $goods['off'] = $user->goods()->where(['onshelf_status'=>'0'])->count();
        $this->success('',$goods);
    }

    /**
     * 我的服务列表
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function mylist()
    {
        [$where, $sort, $order, $offset, $limit] = $this->buildparams($this->model, '', '', '');
        $total = $this->model
            ->where($where)
            ->where('user_id','=', $this->auth->id)
            ->count();
        $list = $this->model
            ->where($where)
            ->where('user_id','=', $this->auth->id)
            ->order($sort, $order)
            ->limit($offset, $limit)
            ->select();

        $result = array("total" => $total, "rows" => $list);
        $this->success('', $result);
    }

    /**
     * 上架服务
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function upsale()
    {
        $id = input('param.id',0, 'intval');
        if (!$id){
            $this->error('参数错误');
        }
        if (!$row = $this->model::get($id)) {
            $this->error('未找到该服务');
        }
        if ($row->user_id != $this->auth->id){
            $this->error('没有权限');
        }
        if ($row->onshelf_status == 1){
            $this->error('不要重复上架');
        }
        $row->onshelf_status = 1;
        $row->verify_status = 'await';
        $row->save();
        $this->success('上架成功，请等待审核');
    }

    /**
     * 下架服务
     * @throws \think\exception\DbException
     */
    public function downsale()
    {
        $id = input('param.id',0, 'intval');
        if (!$id){
            $this->error('参数错误');
        }
        if (!$row = $this->model::get($id)) {
            $this->error('未找到该服务');
        }
        if ($row->user_id != $this->auth->id){
            $this->error('没有权限');
        }
        if ($row->onshelf_status == 0){
            $this->error('不要重复下架');
        }
        $row->onshelf_status = 0;
        $row->save();
        $this->success('下架成功');
    }


}